package com.lotter.api.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lotter.common.exception.LotterCusException;
import com.lotter.common.invocation.BaseConditionVO;
import com.lotter.dao.ClickRecordMapper;
import com.lotter.dao.HourseNewsMapper;
import com.lotter.dao.HousenoteMapper;
import com.lotter.dao.UserAccountMapper;
import com.lotter.model.comment.ClickRecord;
import com.lotter.model.hoursenew.HouserNews;
import com.lotter.model.housenote.Housenote;
import com.lotter.model.user.UserAccount;

@Service
public class HousenoteService {
	
	@Autowired
	private HousenoteMapper housenoteMapper ;
	
	@Autowired
	private HourseNewsMapper hourseNewsMapper;
	
	@Autowired
	private UserAccountMapper userAccountMapper;
	
	@Autowired
	private ClickRecordMapper clickRecordMapper;
	
	
	/**
	 * 保存房间评论信息
	 * @param houserNews
	 * @throws LotterCusException
	 */
	@Transactional(rollbackFor=Exception.class)
	public void insert(Long uid,String content,Long hid) throws LotterCusException{
		UserAccount userAccount = userAccountMapper.selectByPrimaryKey(uid);
		Housenote housenote = new Housenote();
		housenote.setCreatedate(new Date());
		housenote.setUid(uid);
		housenote.setUname(userAccount.getNickname());
		housenote.setStepnum(0L);
		housenote.setTopnum(0L);
		housenote.setContent(content);
		housenote.setHid(hid);
		housenoteMapper.insert(housenote);
	}
	
	/**
	 * 查询房间信息
	 * @param result
	 * @param vo
	 * @return
	 * @throws LotterCusException
	 */
	public Map<String,Object> findHousenoteList( Map<String,Object> result,
			Long uid,Long hid) 
			throws LotterCusException{
		HouserNews hourseNews = hourseNewsMapper.selectByPrimaryKey(hid);
		result.put("hourseNews", hourseNews);
		BaseConditionVO vo = new BaseConditionVO();		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("uid", uid);
		condition.put("hid", hid);
		vo.setCondition(condition);
		List<Housenote> list = housenoteMapper.findHousenoteByPage(vo);
		result.put("data", list);
		return result;
	}
	
	/**
	 * 顶一下 或者踩一下
	 * @param result
	 * @param id
	 * @param type
	 * @return
	 * @throws LotterCusException
	 */
	@Transactional(rollbackFor=Exception.class)
	public void updateNoteTopORFoot(Long uid,Long nid ,int type)
	    throws LotterCusException{
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("id", nid);
		result.put("type", type);
		housenoteMapper.updateTopFoot(result);
		
		ClickRecord clickRecord = new ClickRecord();
		clickRecord.setNid(nid);
		clickRecord.setUid(uid);
		clickRecord.setCreatedate(new Date());
		clickRecordMapper.insert(clickRecord);
	}

	/**
	 * 根据主键查询数据
	 * @param id
	 * @return
	 * @throws LotterCusException
	 */
	public Housenote selectByPrimaryKey(Long id) throws LotterCusException{
		return housenoteMapper.selectByPrimaryKey(id);
	}
}
